﻿namespace iFinance.Modules.Outgoings.ViewModels
{
    using System.Collections.ObjectModel;
    using System.Linq;
    using iFinance.Entity;
    using iFinance.Infrastructure;
    using iFinance.Infrastructure.Services;
    using iFinance.Infrastructure.ViewModels;
    using Microsoft.Practices.Unity;

    /// <summary>
    /// 支出明细项ViewModel
    /// </summary>
    public class OutgoingsDetailItemViewModel : ValidationNotificationObject
    {
        #region Ctor

        public OutgoingsDetailItemViewModel()
            : this(new OutgoingsDetail())
        {
        }

        public OutgoingsDetailItemViewModel(OutgoingsDetail outgoingsDetail)
        {
            this.RealData = outgoingsDetail;
            //初始化消费品
            InitConsumable(outgoingsDetail);
        }        

        #endregion

        #region 属性

        /// <summary>
        /// 代理的实际数据
        /// </summary>
        public OutgoingsDetail RealData { get; set; }

        /// <summary>
        /// 单价
        /// </summary>
        public decimal UnitPrice
        {
            get { return RealData.UnitPrice; }
            set
            {
                if (RealData.UnitPrice != value)
                {
                    RealData.UnitPrice = value;
                    RaisePropertyChanged(() => this.UnitPrice);
                }
            }
        }

        /// <summary>
        /// 数量
        /// </summary>
        public decimal Count
        {
            get { return RealData.Count; }
            set
            {
                if (RealData.Count != value)
                {
                    RealData.Count = value;
                    RaisePropertyChanged(() => this.Count);
                }
            }
        }

        private string consumableName = string.Empty;
        /// <summary>
        /// 消费品名
        /// </summary>
        public string ConsumableName
        {
            get { return this.consumableName; }
            set
            {
                if (this.consumableName != value)
                {
                    this.consumableName = value;
                    RaisePropertyChanged(() => this.ConsumableName);
                }
            }
        }

        private ConsumableItemViewModel currentConsumable = null;
        /// <summary>
        /// 当前选中的消费品
        /// </summary>
        public ConsumableItemViewModel CurrentConsumable
        {
            get { return this.currentConsumable; }
            set
            {
                if (this.currentConsumable != value)
                {
                    this.currentConsumable = value;
                    if (this.currentConsumable != null)
                    {
                        this.UnitPrice = this.CurrentConsumable.LastUnitPrice;
                        this.RealData.ConsumableID = this.CurrentConsumable.ID;
                    }
                    RaisePropertyChanged(() => this.CurrentConsumable);
                }
            }
        }

        private ConsumableViewModel consumables = null;
        /// <summary>
        /// 消费品列表
        /// </summary>
        public ConsumableViewModel Consumables
        {
            get { return this.consumables; }
            set
            {
                if (this.consumables != value)
                {
                    this.consumables = value;
                    RaisePropertyChanged(() => this.Consumables);
                }
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化消费品
        /// </summary>
        /// <param name="outgoingsDetail"></param>
        private void InitConsumable(OutgoingsDetail outgoingsDetail)
        {
            //获取消费品列表
            var basicInfoService = UnityHelper.Container.Resolve<IBasicInfoService>();
            if (basicInfoService != null)
            {
                this.Consumables = basicInfoService.GetAllConsumable();
                //初始化消费品选择
                if (this.Consumables != null)
                {
                    if (outgoingsDetail != null)
                    {
                        var consumable = this.Consumables.List.FirstOrDefault(c => { return c.ID.Equals(outgoingsDetail.ConsumableID); });
                        this.CurrentConsumable = consumable;
                    }

                    if (this.CurrentConsumable == null && this.Consumables.List.Count > 0)
                    {
                        this.CurrentConsumable = this.Consumables.List[0];
                    }
                }
            }
        }

        #endregion
    }

    /// <summary>
    /// 支出明细列表ViewModel
    /// </summary>
    public class OutgoingsDetailListViewModel : ObservableCollection<OutgoingsDetailItemViewModel>
    {
    }
}